package server.content.skills;

import core.util.Misc;
import server.game.players.Client;

/**
 * 
 * @author Miracle
 *
 */
public class MineEss {

	private final Client c;
	private boolean isActive;
	private int mineAnim = -1;
	private final int PICKAXES[] = { 1265, 1267, 1269, 1271, 1273, 1275 };

	public MineEss(final Client c) {
		this.c = c;
	}

	public void startMine() {
		if (getClient().getItems().freeSlots() < 1) {
			getClient().sendMessage("You're inventory is full.");
			reset();
			return;
		}
		if (!hasPickAxe()) {
			getClient().sendMessage("You need a pickaxe to mine essence.");
			reset();
			return;
		}
		setAnim(getMineAnim(getMyPickAxe()));
		setActive(true);
	}

	public void process() {
		getClient().startAnimation(getAnim());
		getClient().getPA().requestUpdates();
		final int outOf = Misc.random(100);
		final int chance = Misc.random(25);
		if (getClient().getItems().freeSlots() < 1) {
			getClient().sendMessage("You're inventory is full.");
			reset();
			return;
		}
		if (!hasPickAxe()) {
			getClient().sendMessage("You need a pickaxe to mine essence.");
			reset();
			return;
		}
		if (outOf <= chance) {
			getClient().getItems().addItem(1436, 1);
			getClient().getPA().addSkillXP(35, 14);
			getClient().sendMessage("You manage to mine some essence.");
		}
	}

	public void reset() {
		getClient().startAnimation(65535);
		getClient().getPA().requestUpdates();
		setAnim(-1);
		setActive(false);
	}

	public boolean hasPickAxe() {
		for (int i = 0; i < PICKAXES.length; i++) {
			if (getClient().getItems().playerHasItem(PICKAXES[i])) {
				return true;
			}
			if (getClient().playerEquipment[getClient().playerWeapon] == PICKAXES[i]) {
				return true;
			}
		}
		return false;
	}

	public int getMyPickAxe() {
		for (int i = 0; i < PICKAXES.length; i++) {
			if (getClient().getItems().playerHasItem(PICKAXES[i])) {
				return PICKAXES[i];
			}
			if (getClient().playerEquipment[getClient().playerWeapon] == PICKAXES[i]) {
				return PICKAXES[i];
			}
		}
		return -1;
	}

	public int getMineAnim(final int PICKAXE) {
		switch (PICKAXE) {
		case 1265:
			return 625;
		case 1267:
			return 627;
		case 1269:
			return 627;
		case 1271:
		case 1273:
			return 629;
		case 1275:
         
		}
		return -1;
	}

	public void setAnim(final int mineAnim) {
		this.mineAnim = mineAnim;
	}

	public int getAnim() {
		return mineAnim;
	}

	public void setActive(final boolean isActive) {
		this.isActive = isActive;
	}

	public boolean isActive() {
		return isActive;
	}

	public Client getClient() {
		return c;
	}

}
